#!/usr/bin/bash
##############################################################
# License Terms
# 
# "This software" is SimpleSMF, or sometimes spelled simplesmf, presently available from http://code.google.com/p/simplesmf
# 
# Copyright (c) 2012 CleverTrove, LLC
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##############################################################
#
PATH=/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin
#
start() {
  su - $VNCUSER -c "vncserver $VNCDISPLAYNUM $VNCOPTIONS"

  # Unfortunately, the exit status of vncserver is undocumented, and if you try to start
  # vncserver when there's already one running, the exit status is 29, which puts service
  # into maintenance mode, which is annoying. So we exit 0 unconditionally, which means
  # service will always be "online" and never enter maintenance. It would be nice to return
  # a more meaningful exit status, so we could enter maintenance if there were a real problem,
  # but without documentation ... it's tough for me to do anything more intelligent.
  exit 0
}
stop() {
  su - $VNCUSER -c "vncserver -kill $VNCDISPLAYNUM"

  # Unfortunately, the exit status of vncserver is undocumented, and if you try to kill
  # vncserver when there isn't any running, the exit status is 2, which puts service
  # into maintenance mode, which is annoying. So we exit 0 unconditionally, which means
  # service will always be "offline" and never enter maintenance. It would be nice to return
  # a more meaningful exit status, so we could enter maintenance if there were a real problem,
  # but without documentation ... it's tough for me to do anything more intelligent.
  exit 0
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  refresh)
    start
    ;;
  *)
    echo "Usage: $0 start|stop|restart|refresh"
esac
